<script setup lang="ts">
import ComHeaderVue from '@/components/ComHeader.vue'
import GoodsList from '@/components/GoodsList.vue'
import { useBanner, useGridList, useSecSkill, useGoodsList } from '@/hooks/homeHook'

const { gridList, handleGriditemClick } = useGridList()
const bannerList = await useBanner()
const { hour, time, miaoshaList } = await useSecSkill()
const {
  goodsList,
  loading,
  finished,
  getGoodsData,
  formatTime,
  isLoading,
  refresh,
} = await useGoodsList()
console.log(bannerList, hour, time, miaoshaList)
</script>

<template>
  <div>
    <ComHeaderVue />
    <van-pull-refresh v-model="isLoading" @refresh="refresh">
      <!-- 轮播图功能 -->
      <div class="banner" v-if="bannerList.length > 0">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="{ bannerid, img, alt } in bannerList" :key="bannerid">
            <img :src="img" :alt="alt" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <!-- 网格模块 -->
      <div class="nav">
        <van-grid column-num="5" icon-size="35" clickable :border="false">
          <van-grid-item
            class="temp"
            v-for="item in gridList"
            :key="item.id"
            :icon="item.url"
            :text="item.text"
            @click="handleGriditemClick(item.id)"
          />
        </van-grid>
      </div>
      <!-- 秒杀功能 -->
      <div class="miaosha">
        <div class="miaosha-header">
          <h3 class="miaosha-title">京东秒杀</h3>
          <div class="miaosha-hour">{{ formatTime(hour) }}点场</div>
          <van-count-down :time="time">
            <template #default="timeData">
              <span class="block">{{ formatTime(timeData.hours) }}</span>
              <span class="colon">:</span>
              <span class="block">{{ formatTime(timeData.minutes) }}</span>
              <span class="colon">:</span>
              <span class="block">{{ formatTime(timeData.seconds) }}</span>
            </template>
          </van-count-down>
        </div>

        <div class="miaosha-content">
          <div class="content-item" v-for="item in miaoshaList" :key="item.proid">
            <img :src="item.img1" alt="" />
            <p class="price">${{ item.originprice }}</p>
          </div>
        </div>

        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getGoodsData"
        >
          <GoodsList ref="goodList" :goodsList="goodsList" />
        </van-list>
      </div>
    </van-pull-refresh>
  </div>
</template>

<style lang="scss" scoped>
.banner {
  box-sizing: border-box;
  margin: 20px;
  border-radius: 10px;
  overflow: hidden;
  .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    text-align: center;
    background-color: #39a9ed;
    img {
      display: block;
      width: 100%;
    }
  }
}
.nav {
  :deep(.van-grid-item__content) {
    background: transparent;
    padding: 16px 7px;
  }
}
.miaosha {
  margin: 10px;
  border-radius: 15px;
  overflow: hidden;
  background: #fff;
  padding: 10px 0 5px;
  .miaosha-header {
    display: flex;
    align-items: center;
    margin: 0px 0 10px;
    height: 40px;
    line-height: 42px;
    font-size: 15px;

    .miaosha-title {
      font-size: 17px;
      font-weight: bolder;
      width: 150px;
      height: 100%;
      text-align: center;
      padding-left: 5px;
    }
    .miaosha-hour {
      color: #ee0a24;
      font-weight: bold;
      height: 100%;
      margin-right: 15px;
    }
    .van-count-down {
      width: 176px;
      // background: #39a9ed;
      transform: translateY(-3px);
      .colon {
        display: inline-block;
        margin: 0 4px;
        color: #ee0a24;
      }
      .block {
        display: inline-block;
        width: 45px;
        height: 28px;
        line-height: 32px;
        font-weight: bold;
        border-radius: 5px;
        color: #fff;
        text-align: center;
        background-color: #ee0a24;
      }
    }
  }
  .miaosha-content {
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;
    box-sizing: border-box;
    .content-item {
      width: 100px;
      img {
        width: 100%;
        height: 100px;
      }
      .price {
        // background: orange;
        margin: 5px auto 5px;
        font-weight: bold;
        color: #f2270c;
        text-align: center;
      }
    }
  }
}
</style>
